﻿using CompanyManagement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace CompanyManagement.usercontrol
{
    public partial class UcEditCompany : System.Web.UI.UserControl
    {
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            RadDateCompanyCreated.MaxDate = DateTime.Now;
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///          Required method for Designer support - do not modify
        ///          the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.DataBinding += new System.EventHandler(this.CompanyDetails_DataBinding);

        }
        #endregion
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void CompanyDetails_DataBinding(object sender, System.EventArgs e)
        {
            //var listDistrict = new DistrictDAO().GetDistricts();
            //ddlDistrict.DataSource = listDistrict;
            //ddlDistrict.DataBind();
            var listType = new CompanyTypeDAO().GetCompanyTypes();
            ddlCompanyType.DataSource = listType;
            ddlCompanyType.DataTextField = "Name";
            ddlCompanyType.DataValueField = "Code";
            ddlCompanyType.DataBind();

            //// Load year
            int currentYear = DateTime.Now.Year;
            ddlYear.Items.Add(new ListItem("Năm", "-1"));
            for (int i = -50; i < 1; i++)
            {
                ListItem item = new ListItem((currentYear + i).ToString(), (currentYear + i).ToString());
                ddlYear.Items.Add(item);
            }
            if (!(DataItem is GridInsertionObject))
            {
                ////Load ward
                //if (listWard.Count > 0)
                //{
                //    object ward = DataBinder.Eval(DataItem, "WardCode");
                //    if (ward == null)
                //    {
                //        ward = listWard.Count > 0 ? listWard[0].Code : "0";
                //    }
            
                //    var wardObj = listWard.FirstOrDefault(p => p.Code.Equals((string)ward));
                //    ddlWard.SelectedIndex = listWard.IndexOf(wardObj);
                //    ddlWard.DataSource = null;
                //}

                //// load district
                //if (listDistrict.Count > 0)
                //{
                //    object district = DataBinder.Eval(DataItem, "DistrictCode");
                //    if (district == null)
                //    {
                //        district = listDistrict.Count > 0 ? listDistrict[0].Code : "0";
                //    }
                //    var districtObj = listDistrict.FirstOrDefault(p => p.Code.Equals((string)district));
                //    ddlDistrict.SelectedIndex = listDistrict.IndexOf(districtObj);
                //    ddlDistrict.DataSource = null;
                //}

                // load Type
                if (listType.Count > 0)
                {
                    object companyType = DataBinder.Eval(DataItem, "CompanyTypeCode");
                    if (companyType == null)
                    {
                        companyType = listType.Count > 0 ? listType[0].Code : "0";
                    }
            
                    var companyTypeObj = listType.FirstOrDefault(p => p.Code.Equals((string)companyType));
                    ddlCompanyType.SelectedIndex = listType.IndexOf(companyTypeObj);
                    ddlCompanyType.DataSource = null;
                }

                object dissolvedDate = DataBinder.Eval(DataItem, "DissolvedDate");
                if (dissolvedDate != null)
                {
                    dissolvedDate = dissolvedDate.ToString().Split('T').Last();
                    ddlMonth.SelectedItem.Text = dissolvedDate.ToString().Split('/').First();
                    ddlYear.SelectedItem.Text = dissolvedDate.ToString().Split('/').Last();
                }

                object status = DataBinder.Eval(DataItem, "Status");
                if (status != null)
                {
                    ddlStatus.SelectedItem.Value = status.ToString();
                }

                object companyCreatedDate = DataBinder.Eval(DataItem, "CompanyCreateDated");
                if (companyCreatedDate != null)
                {
                    RadDateCompanyCreated.SelectedDate = ConvertToDateTime(companyCreatedDate.ToString());
                }
            }

            //object dissolvedDate = DataBinder.Eval(DataItem, "DissolvedDate");
            //if (dissolvedDate != null)
            //{
                
            //}
        }

        private DateTime? ConvertToDateTime(string createdDate)
        {
            try
            {
                string[] arr = createdDate.Split('/');
                return new DateTime(Convert.ToInt32(arr[2]), Convert.ToInt32(arr[1]), Convert.ToInt32(arr[0]));
            }
            catch (Exception)
            {
                Tools.Logger(string.Format("Error: Date:{0}", createdDate), "CompanyManagement", "ConvertToDateTime");
                return new DateTime();
            }
        }
        public string ProcessMyDataItem(object myValue)
        {
            if (myValue == null)
            {
                return "";
            }

            return myValue.ToString();
        }

        //protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string districtCode = ddlDistrict.SelectedValue;
        //    LoadWards(districtCode);
        //}

        //private void LoadWards(string districtCode)
        //{
        //    var listWard = new WardDAO().GetWards(districtCode);
        //    ddlWard.DataSource = listWard;
        //    ddlWard.DataBind();
        //}
    }
}